package com.sunducation.wts.room

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.sunducation.wts.room.bean.Address
import com.sunducation.wts.room.bean.Book
import com.sunducation.wts.room.bean.User

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //数据库初始化，可以在application中，也可以在单例中
        //MyApplication.db.userDao().insertAll(User(null, "c1", "c2",  null))
        RoomBuilder.getDb(this).userDao().insertAll(
            User(
                null,
                "a1",
                "a2",
                null,
                Address("s1", "s2", "s3", 1),
                Address("t1", "t2", "t3", 2)
            )
        )
        RoomBuilder.getDb(this).bookDao().insertAll(
            Book(
                null,
                "书1",
                1
            )
        )
        val u: User = RoomBuilder.getDb(this).userDao().queryUserById(1)
        //RoomBuilder.getDb(this).userDao().delete(u) //会报错，先将book删除，才可以删除user

    }
}
